<?php
ifi!perms_check('admin', 'config')) {
	redirect('index.php?module=error&error=auth_error');
	exit;
}

if(!$_GET['cmd'])
{
	SQLvalidate($_GET['rec']);

	//sort
	if($_GET['sort']!='ASC' && $_GET['sort']!='DESC')
		$_GET['sort']='ASC';
	if($_GET['sortby']!='name')
		$_GET['sortby']='name';
	if($_GET['sort']=='ASC')
		$sortnext='DESC';
	else
		$sortnext='ASC';
	//

	//pages
	if(!$_GET['rec'])
		$_GET['rec']=0;
	//

	$languages = get_lang_list();

	//search
	if($_POST['search'] or $_GET['search'])
	{
		if($_GET['search'])
			$_POST['search']=$_GET['search'];

		$_POST['search']=read_text_rest($_POST['search']);

		$srch=str_replace('*', '%', $_POST['search']);//* na % dla znakow dowolnych
		$s=array();
		foreach($languages as $l_f)
			$s[]="$l_f LIKE '%$srch%'";

		$srch="WHERE ".implode(" || ", $s)." || name LIKE '%$srch%'";
	} 
	//  

	$db->query("SELECT * FROM $conf[prefix]lang $srch") or $db->err(__FILE__, __LINE__);
	$q=$db->num_rows();
	$db->query("SELECT * FROM $conf[prefix]lang $srch ORDER by name $_GET[sort] LIMIT $_GET[rec], $conf[admin_per_page]") or $db->err(__FILE__, __LINE__);

	//page link
	$page_link=split_to_pages('<a href="index.php?module=admin&amp;action=lang&amp;sortby='.$_GET['sortby'].'&amp;sort='.$_GET['sort'].'&amp;search='.$_POST['search'].'&amp;rec={rec}">{nr}</a>', $conf['admin_per_page'], $q, $_GET['rec']);

	$page_link='
		<table width="100%" border="0">
		<tr>
		<td>'.$page_link.'</td>
		</tr>
		</table>';

	//search
	$content='
		<table border="0" width="100%">
		<tr>
		<form method="post" action="index.php?module=admin&amp;action=lang">
		<td><input type="text" size="15" class="button" value="'.$_POST['search'].'" name="search" /> <input type="submit" value="'.$GLOBALS['lang']['global']['search'].'" class="button" /></td>
		</form>
		</tr>
		<tr>
		<td>'.$GLOBALS['lang']['global']['hint_search'].'</td>
		</tr>
		</table><br />';
	//  

	$content.='
		<form action="index.php" method="post">
		<input type="hidden" name="module" value="lang">
		<input type="hidden" name="function" value="lang_edit">
		<table border="0" width="100%" class="tabelka2">
		<tr>
		<td class="row_3" colspan="2"><a href="index.php?module=admin&action=lang&sortby=name&sort='.$sortnext.'&amp;search='.$_post['search'].'">'.$lang['conf_name'].'</a></td>
		</tr>';

	if($db->num_rows()==0)
		$content.='
			<tr>
			<td align="center" colspan="2" class="row_1">'.$lang['no_matches'].'</td>
			</tr>	
			';

	for($x=0;$x<$db->num_rows();$x++)
	{
		$i++;
		if($i>2)
			$i=1;

		$l=$db->fetch_object();

		$content.='
			<tr>
			<td align="left" colspan="2"><h3>$lang[\''.$l->name.'\']</h3></td>
			</tr>';
		$type='';
		$y=0;
		foreach($languages as $l_f) {
			if($l_f==$_SESSION['lang_short']) {
				$icon = $GLOBALS['theme_img']['collapse'];
				$style = 'block';
			}
			else {
				$icon = $GLOBALS['theme_img']['expand'];
				$style = 'none';
			}

			$l->$l_f=str_replace("<?","[php]", $l->$l_f);
			$l->$l_f=str_replace("?>","[/php]", $l->$l_f);
			$l->$l_f=str_replace("<","&amp;lt;", $l->$l_f);
			$l->$l_f=str_replace(">","&amp;gt;", $l->$l_f);
			$l->$l_f=read_text_edit($l->$l_f);
			$type.='<tr valign="top"><td class="row_'.$i.'" align="left"><a href="#" onclick="showhide(\''.$x.$y.'\', \''.$GLOBALS['theme_path'].$GLOBALS['theme_img']['expand'].'\', \''.$GLOBALS['theme_path'].$GLOBALS['theme_img']['collapse'].'\'); return false;"><b><span id="switch_'.$x.$y.'"><img src="'.$GLOBALS['theme_path'].$icon.'" alt="" /></span> '.$l_f.'</a><br /><div id="multi_'.$x.$y.'" style="display:'.$style.'"><textarea cols="70" rows="5" name="'.$l->name.'_'.$l_f.'" class="button">'.(read_text_rest($l->$l_f)).'</textarea></div></td></tr>';	
			$y++;
		}     


		$content.=$type;
	}

	$content.='
		<tr>
		<td class="row_3"><input type="submit" value="'.$lang['admin_edit'].'" class="button"></td></form>
		</tr>
		</table>'.$page_link;
}
elseif($_GET['cmd'] == 'installer') {
	if(!perms_check('admin', 'modules')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	if($_GET['del'] == 1) {
		SQLvalidate($_GET['lang'], 'varchar', 4);

		$av_langs = get_lang_list();

		if(!$_GET['language'] or !in_array($_GET['language'], $av_langs) or $_GET['language'] == $conf['language']) {
			redirect($_SESSION['redirect_2']);
			exit();
		}

		$db = new dbquery;
		$db->query("SELECT $_GET[language] FROM $conf[prefix]lang WHERE name = 'langname'");
		$d = $db->fetch_object();

		$yes='<input type="button" onClick="javascript:location.href=\'index.php?module=lang&function=language_uninstall&amp;language='.$_GET['lang'].'\'" value="'.$lang['yes'].'" class="button" />';
		$no='<input type="button" onClick="javascript:location.href=\'index.php?module=admin&action=lang&amp;cmd=installer\'" value="'.$lang['no'].'" class="button" />';

		$t = $d->$_GET['language'];

		$content = string_template($lang['admin_delete_lang'], array('yes' => $yes, 'no' => $no, 'lang' => $t));
	}
	else {
		$i=0;

		$content='
			<table align="center" width="75%" border="0" class="tabelka2">
			<tr>
			<td class="row_3" align="center" width="50%">'.$lang['language'].'</td>
			<td class="row_3" align="center">'.$lang['admin_options'].'</td>
			</tr>';

		$av_langs = get_lang_list();

		$db = new dbquery;
		$db->query("SELECT * FROM $conf[prefix]lang WHERE name = 'langname'");
		$row = $db->fetch_assoc();

		foreach($av_langs as $l) {
			if($i > 1)
				$i = 1;
			else
				$i++;
			if($l != $conf['language'])
				$options = '<a href="index.php?module=admin&action=lang&cmd=installer&del=1&amp;language='.$l.'">'.$lang['admin_uninstall'].'</a>';
			else
				$options = '-';
			if(file_exists("$l.lang"))
				$options .= '<br /><a href="index.php?module=admin&action=lang&cmd=installer&upd=1&amp;lang='.$l.'">'.$lang['admin_update'].'</a>';
			$content.='
				<tr>
				<td class="row_'.$i.'" align="center">'.$row[$l].'</td>
				<td class="row_'.$i.'" align="center">'.$options.'</td>
				</tr>';
		}

		$d = dir('.');

		while($fl = $d->read()) {
			if(ereg("^([a-z]+)\.lang$", $fl, $match)) {
				$l = $match[1];
				if(!in_array($l, $av_langs)) {
					if($i > 1)
						$i = 1;
					else
						$i++;
					include_once($fl);
					$options='<a href="index.php?module=lang&function=language_install&amp;language='.$l.'">'.$lang['admin_install'].'</a>';
					$content.='
						<tr>
						<td class="row_'.$i.'" align="center">'.$language_upd['langname'].'</td>
						<td class="row_'.$i.'" align="center">'.$options.'</td>
						</tr>';
				}
			}
		}

		$content.='</table>';
	}
}

$content=string_template(read_file('middle.php'), array('theme_path' => $GLOBALS['theme_path'], 'content' => $content, 'description' => $lang['language']));
?>
